//
//  GuideView.m
//  ZaloChatPlugin
//
//  Created by HuyNC on 10/25/13.
//  Copyright (c) 2013 NCH. All rights reserved.
//

#import "GuideView.h"

@interface GuideView(){
    NSMutableArray* pageViews;
    NSString* userDefaultKey;
    
    bool isShowIndicator;
    bool isAutoScroll;
    float transparent;
    
    int pageIndex;
    NSTimer* changeTimer;
}

@end

@implementation GuideView

@synthesize isShowIndicator, isAutoScroll, userDefaultKey, transparent, delegate;

- (id)initWithFrame:(CGRect)frame userDefaultKey:(NSString *)_userDefaultKey
{
    self = [super initWithFrame:frame];
    if (self) {
        pageViews = [[NSMutableArray alloc] init];
        pageIndex = -1;
        isAutoScroll = false;
        isShowIndicator = true;
        self.userDefaultKey = _userDefaultKey;
        self.autoScrollDuration = 5;
        
        self.transparent = 0.7;
    }
    return self;
}

- (void)addPageView:(UIView*)pageView{
    [pageViews addObject:pageView];
    
    if(pageIndex == -1){
        pageIndex++;
        [self addSubview:[pageViews objectAtIndex:pageIndex]];
    }
}

- (void)removePageView:(UIView*)pageView{
    [pageViews removeObject:pageView];
}
- (bool)isShowedBefore{
    return [GuideView isUserDefaultKeyUsed:userDefaultKey];
}

+ (bool)isUserDefaultKeyUsed:(NSString*)userDefaultKey{
    NSNumber* nIsUsed = [[NSUserDefaults standardUserDefaults] objectForKey:userDefaultKey];
    if(nIsUsed)
        return true;
    return false;
}

- (bool)changeNextView{
    [[pageViews objectAtIndex:pageIndex] removeFromSuperview];
    pageIndex++;
    if(pageIndex < pageViews.count){
        [self addSubview:[pageViews objectAtIndex:pageIndex]];
        return true;
    }
    return false;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self changeViewWithFinishAction];
}

- (void)addToSuperViewIfNeed:(UIView*)superView{
    if([self isShowedBefore] == false){
        [self addToSuperView:superView];
    }
}

- (void)addToSuperView:(UIView*)superView{
    [superView addSubview:self];
    [superView bringSubviewToFront:self];
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:userDefaultKey];
    
    // animation
    self.alpha = 0;
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
    }];
    
    if(isAutoScroll) {
        changeTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changeTimerTicked) userInfo:nil repeats:YES];
    }
}

- (void)setTransparent:(float)_transparent{
    transparent = _transparent;
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:transparent];
}

- (void)changeTimerTicked {
    [self changeViewWithFinishAction];
}

- (void)changeViewWithFinishAction {
    bool isChangeSuccess = [self changeNextView];
    
    // auto remove when go to final
    if(isChangeSuccess == false){
        [changeTimer invalidate];
        [self removeFromSuperview];
        if(delegate && [delegate respondsToSelector:@selector(GuideViewDidDismiss:)])
            [delegate GuideViewDidDismiss:self];
    }
}

@end
